package com.htthorizon.powersupply.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.htthorizon.powersupply.dto.LineOutageItem;
import com.htthorizon.powersupply.dto.TgOutageItem;
import com.htthorizon.powersupply.dto.UserOutageItem;
import com.htthorizon.powersupply.mapper.UserOutageMonitorMapper;
import com.htthorizon.powersupply.search.TgOutageItemSearch;
import com.htthorizon.powersupply.search.UserOutageItemSearch;
import com.htthorizon.powersupply.service.RdsGridTgPowerDfService;
import com.htthorizon.powersupply.service.RealTimePowerOutageService;
import com.htthorizon.powersupply.service.UserOutageMonitorService;
import com.htthorizon.powersupply.utils.OrgUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserOutageMonitorServiceImpl implements UserOutageMonitorService {

    @Autowired
    private UserOutageMonitorMapper userOutageMonitorMapper;
    @Autowired
    private RealTimePowerOutageService realTimePowerOutageService;
    @Autowired
    private RdsGridTgPowerDfService rdsGridTgPowerDfService;

    @Override
    public List<LineOutageItem> getStatistics(String type, String value) {
        List<LineOutageItem> lineOutageItems = userOutageMonitorMapper.getStatistics(type,value);
        if (CollectionUtils.isNotEmpty(lineOutageItems)) {
            lineOutageItems.forEach(item -> {
                item.setStdCountyOrgNm(OrgUtil.map(item.getStdCountyOrgNm()));
                item.setStdOrgNm(OrgUtil.getStdOrgNm(item.getStdOrgNm()));
                item.setLabel(rdsGridTgPowerDfService.lineNmLabel(item.getLineNm()));
            });
        }
        return lineOutageItems;
    }



    @Override
    public List<UserOutageItem> getUserOutageItems(UserOutageItemSearch search) {
        List<UserOutageItem> userOutageItems = userOutageMonitorMapper.getUserOutageItems(search);
        if (CollectionUtils.isNotEmpty(userOutageItems)) {
            userOutageItems.forEach(item -> item.setStdCountyOrgNm(OrgUtil.map(item.getStdCountyOrgNm())));
        }
        return userOutageItems;
    }

    @Override
    public List<TgOutageItem> getTgOutageItems(TgOutageItemSearch tgOutageItemSearch) {
        List<TgOutageItem> tgOutageItems =  userOutageMonitorMapper.getTgOutageItems(tgOutageItemSearch);
        if (CollectionUtils.isNotEmpty(tgOutageItems)) {
            tgOutageItems.forEach(item -> {
                item.setStdCountyOrgNm(OrgUtil.map(item.getStdCountyOrgNm()));
                item.setStdCountyOrgNm(rdsGridTgPowerDfService.tgLabel(item.getTgNo()));
            });
        }
        return tgOutageItems;
    }
}
